package server

import (
	"fmt"
	"log"
	"math/rand"
	"time"
)

type Card struct {
	Number    uint8 `json:"number"`    //牌值
	Varieties uint8 `json:"varieties"` //花色
}

var originCardArray [52]Card

func init() {
	//初始化牌
	var index = 0
	for i := 0; i < 4; i++ {
		for j := 1; j <= 13; j++ {
			card := Card{
				uint8(j),
				uint8(i),
			}
			originCardArray[index] = card
			index++
		}
	}

	log.Println("poker init")
}

func Shuffle() []Card {
	array := make([]Card, 52, 52)
	copy(array, originCardArray[:])
	randomExchange(array)

	//切牌
	rand := rand.New(rand.NewSource(time.Now().UnixNano()))
	index := rand.Intn(len(array))

	array = append(array[index:], array[0:index]...)

	return array
}


//洗牌算法
func randomExchange(array []Card) {
	exchangeCounts := 4 * len(array) // 进行 数组长度*4 次交换
	for i := 0; i < exchangeCounts; i++ {
		r := rand.New(rand.NewSource(time.Now().UnixNano()))

		from := int64(r.Intn(len(array)))
		to := int64(r.Intn(len(array)))

		array[from], array[to] = array[to], array[from]
	}
}

func (c *Card) string() string {
	return fmt.Sprintf("%d,%d", c.Number, c.Varieties)
}
